package com.da.javatest.leetCode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chenlida
 * @date 2019/1/12 16:25
 * @description
 */
public class Code337 {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(3);
        TreeNode r11 = new TreeNode(2);
        TreeNode r12 = new TreeNode(3);
        root.left = r11;
        root.right = r12;
        TreeNode r21 = new TreeNode(3);
        r11.right = r21;
        TreeNode r22 = new TreeNode(1);
        r12.right = r22;

        new Code337().rob(root);
    }

    private List<Integer> result = new ArrayList<>();

    public int rob(TreeNode root) {
        int max;
        getMoney(root, 1);
        if (result.size() == 4) {
            max = result.get(0) + result.get(2);
            max = Math.max(result.get(1) + result.get(3), max);
            max = Math.max(result.get(0) + result.get(3), max);
        } else {
            int m1 = 0, m2 = 0;
            for (int i = 0; i < result.size(); i++) {
                if (i % 2 == 0) {
                    m1 += result.get(i);
                } else {
                    m2 += result.get(i);
                }
            }
            max = Math.max(m1, m2);
        }
        System.out.println(result);
        return max;
    }

    private void getMoney(TreeNode node, int level) {
        if (node != null) {
            if (level > result.size()) {
                result.add(node.val);
            } else {
                result.set(level - 1, node.val + result.get(level - 1));
            }
            level++;
            getMoney(node.left, level);
            getMoney(node.right, level);
        }
    }
}
